from functools import lru_cache

from pydantic import BaseSettings

# dotenv_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '.fastenv')
# if os.path.exists(dotenv_path):
#     load_dotenv(dotenv_path, override=True)  # override=True: 覆写已存在的变量
#
# dotenv_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '.env')
# if os.path.exists(dotenv_path):
#     load_dotenv(dotenv_path, override=True)  # override=True: 覆写已存在的变量
from rearq import ReArq


class Settings(BaseSettings):
    app_name: str = "cjserver API"
    SQLALCHEMY_DATABASE_URI: str
    SQLALCHEMY_DATABASE_URI_2: str
    SQLALCHEMY_DATABASE_URI_3: str
    VERSION: str
    MONGODB_URI: str
    MONGODB_DB: str
    MONGODB_COL: str
    MYSQL_HOST_1: str
    MYSQL_PORT_1: int
    MYSQL_USER_1: str
    MYSQL_PWD_1: str
    MYSQL_DATEBASE_1: str
    MONGODB_URI_HTMLJSON: str
    MONGODB_URI_HTMLJSON_DB: str
    MONGODB_URI_HTMLJSONLATEST: str
    MONGODB_URI_HTMLJSONLATEST_DB: str
    REDIS: str
    PROXY_URL: str
    UPDATE_TASK: str
    CHAOXING_COOKIE_GET: str
    PARA_URL: str
    SQL_ADD_URL: str
    SQL_ADD_MANY_URL: str
    SQL_UPDATE_URL: str
    MONGO_SAVE_URL: str
    SELECT_URL: str
    RESET_NEW_DATA_URL: str
    DEL_MONGO_ERR: str
    MONGO_BASE_DIR: str
    MONGO_SAVE_PATH: str
    MONGO_SAVE_MAX_SIZE: int
    MONGO_SAVE_TRY_CNT: int

    class Config:
        env_file = ".env"


@lru_cache()
def get_settings():
    return Settings()


class URLDISTRIBUTED(object):

    @classmethod
    async def get_base_url(cls):
        from app_main import app
        redis = app.state.redis
        url = await redis.srandmember("baseurl")
        if app.state.tags.find("test") > -1:
            return "http://127.0.0.1:" + str(app.state.port)
        else:
            if url:
                return url
            else:
                return "http://127.0.0.1:8001"

    @classmethod
    async def get_para_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().PARA_URL
        return url

    @classmethod
    async def get_sql_add_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().SQL_ADD_URL
        return url

    @classmethod
    async def get_sql_add_many_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().SQL_ADD_MANY_URL
        return url

    @classmethod
    async def get_sql_update_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().SQL_UPDATE_URL
        return url

    @classmethod
    async def get_mongo_save_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().MONGO_SAVE_URL
        return url

    @classmethod
    async def get_select_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().SELECT_URL
        return url

    @classmethod
    async def get_reset_new_data_url(cls):
        url = await cls.get_base_url()
        url = url + get_settings().RESET_NEW_DATA_URL
        return url

    @classmethod
    async def get_task_url(cls):
        url = await cls.get_base_url()
        url = url + "/showinfo/task/showinfo/tasks"
        return url

    @classmethod
    async def get_err_url(cls):
        url = await cls.get_base_url()
        url = url + "/err_analysis_server/err_analysis/errs_analysis/err_client_except_find"
        return url
